import { MVC } from "../../framework/MVC";
import { AlertManager } from "../../alert/AlertManager";
import { GameVoManager } from "../../manager/GameVoManager";
import { UserVo } from "../../shareData/UserVo";
import NetAdapter from "../../adpapter/NetAdapter";
import FightModel from "../fight/FightModel";
import { DropCfg } from "../../config/DropCfg";
import { Cfg } from "../../config/Cfg";
import { RoleManager } from "../../manager/RoleManager";
import { ListenID } from "../../ListenID";
import { Notifier } from "../../framework/Notifier";
import { Manager } from "../../manager/Manager";
import GlobalData from "../../GlobalData";
import { UIManager } from "../../framework/UIManager";
import { Common_UIPath } from "../../common/Common_Define";
import { AudioType } from "../../manager/AudioManager";
import { Util } from "../../utils/Util";

const { ccclass, property } = cc._decorator;
const { Prefab } = cc;
@ccclass
export default class CheatView extends MVC.BaseView {
  public onOpen(args: any): void {
    GlobalData.inst.isChaet = true;
    super.onOpen(args);
    try {
      let label = this.node.getChildByName("checkinGame");
      label.getComponent(
        cc.Label
      ).string = `诀窍记录:${GameVoManager.getInstance.myUserVo.successTips}`;
    } catch (error) {
      console.error(error);
    }
  }

  public onClose(): void {
    super.onClose();
  }

  protected changeListener(enable: boolean): void {}
  public resetUserVo() {
    GameVoManager.getInstance.myUserVo = new UserVo();
    GameVoManager.getInstance.saveAllData();
  }

  public addDay() {
    GameVoManager.getInstance.myUserVo.last_sign_day = 0;
  }

  public addGold() {
    // GlobalData.inst.uid = "62266"
    GameVoManager.getInstance.setGold(10000000000);
  }

  public addDiamond() {
    GameVoManager.getInstance.setDiamond(10000);
  }

  public selectLevel() {
    let editbox = this.node
      .getChildByName("editLevelBox")
      .getComponent(cc.EditBox);
    let num = Number(editbox.string);
    if (num) {
      GameVoManager.getInstance.myUserVo.top_level = num;
      GameVoManager.getInstance.stateVo.curLevel =
      GameVoManager.getInstance.myUserVo.top_level + 1;
      // Manager.map.setMapByChapterId(Util.levelToChapterId(GameVoManager.getInstance.myUserVo.top_level))
      FightModel.getInstance.initLevelCfg(
        GameVoManager.getInstance.stateVo.curLevel
      );
      AlertManager.showNormalTips("选择" + num + "关卡成功");
    } else {
      AlertManager.showNormalTips("关卡数出错");
    }
  }

  public copyOpenId() {
    let str = "";
    GameVoManager.getInstance.saveData();
    if (window["wx"]) {
      wx.setClipboardData({
        data: str,
        success: () => {
          AlertManager.showNormalTips("成功");
        },
        fail: () => {
          AlertManager.showNormalTips("失败");
        },
      });
    }
  }
  private toolCfg: DropCfg = null;
  private hurt: number = 0;
  public toolIcon(evet, curstomdata) {
    if (!FightModel.getInstance.isFighting) {
      AlertManager.showNormalTips("不在战斗中，别乱按哈");
      return;
    }
    this.toolCfg = Cfg.Drop.get(curstomdata);
  }
  public switchRoleFire() {
    RoleManager.getInstance.mainRole.cheat =
      !RoleManager.getInstance.mainRole.cheat;
  }
  // 清除token，强制退出,关闭所有预制体
  public clearToken() {
    // cc.director.loadScene("GameLauncher")
    UIManager.showCurrentUI();
    var currentScene = cc.director.getScene();
    this.bianli(currentScene);
    return
    let clsoe = 0;
    UIManager.showCurrentUI();
    console.log("Scence:" + cc.director.getScene().name);
    if (clsoe > 0) return;
    UIManager.closeAllUI();
    clsoe++;
    return;
    // 清除token
    UIManager.closeAllUI();
    GlobalData.inst.init();
    UIManager.Open(
      Common_UIPath.LoginView,
      MVC.eTransition.Default,
      MVC.eUILayer.Tips
    );

    Manager.audio.playAudio(501, AudioType.UI);
    this.onClose();
    return;
    cc.director.loadScene("GameLauncher", () => {
      var currentScene = cc.director.getScene();
      this.bianli(currentScene);
      // Launcher.logininit()
      // let login  = new Launcher()
      // login.logininit()
    });
  }

  bianli(node) {
    node.children.forEach((child) => {
      console.log(child);
      this.bianli(child);
      if (child === null) return;
    });
  }

  public addChest() {
    // GameVoManager.getInstance.myUserVo.treasureBox++;
    Notifier.send(ListenID.Fight_GetExpAnimation, 100);
  }

  public cleanFunUnlockFlag() {
    for (let i = 1; i <= 50; ++i) {
      Manager.storage.setBool("funUnlock_" + i, false);
    }
    AlertManager.showNormalTips("清除成功");
  }

  public showRandomAnnounce() {
    Notifier.send(ListenID.Announce_LogStartNum, 2);
    Notifier.send(ListenID.MainDrawClose);
  }

  /**
   * 攻击模式切换
   */

  public switchFireMode(event, curstomdata) {
    FightModel.getInstance.fireMode = !FightModel.getInstance.fireMode;
    console.log("FightMode firemode", FightModel.getInstance.fireMode);
  }

  public addSkill(event) {
    let editbox = this.node
      .getChildByName("editSkillBox")
      .getComponent(cc.EditBox);
    let num = Number(editbox.string);
    if (num) {
      Notifier.send(ListenID.Fight_AddSkill, num);
    }
  }

  public addRoleExp() {
    GameVoManager.getInstance.addExp(100);
  }

  public addEquip() {
    let editbox = this.node
      .getChildByName("editSkillBox")
      .getComponent(cc.EditBox);
    let num = Number(editbox.string);
    if (num) {
      GameVoManager.getInstance.addEquip(num);
      return
    }
    GameVoManager.getInstance.addEquip(88004);
  }

  public btnHero(){
    GameVoManager.getInstance.addEquip(10612);
    GameVoManager.getInstance.addEquip(10645);
    GameVoManager.getInstance.addEquip(10700);
    GameVoManager.getInstance.addEquip(99999);

    GameVoManager.getInstance.setGold(1000000000000);
    GameVoManager.getInstance.addExp(1000);
    GameVoManager.getInstance.myUserVo.top_level = 250;
    if(FightModel.getInstance.isFighting){
      for(let i = 0; i < 8; i++){
        Notifier.send(ListenID.Fight_AddSkill, 8005);
        Notifier.send(ListenID.Fight_AddSkill, 8006);
        Notifier.send(ListenID.Fight_AddSkill, 8007);
        Notifier.send(ListenID.Fight_AddSkill, 8008);
        Notifier.send(ListenID.Fight_AddSkill, 8009);
        Notifier.send(ListenID.Fight_AddSkill, 8010);
        Notifier.send(ListenID.Fight_AddSkill, 8011);
        Notifier.send(ListenID.Fight_AddSkill, 8012);
        Notifier.send(ListenID.Fight_AddSkill, 8017);
        Notifier.send(ListenID.Fight_AddSkill, 8025);
        Notifier.send(ListenID.Fight_AddSkill, 8026);
        Notifier.send(ListenID.Fight_AddSkill, 8027);
        Notifier.send(ListenID.Fight_AddSkill, 8028);
        Notifier.send(ListenID.Fight_AddSkill, 8032);
        Notifier.send(ListenID.Fight_AddSkill, 8033);
        Notifier.send(ListenID.Fight_AddSkill, 8036);

      }
    }
  GlobalData.inst.isChaet = true;
  }
}
